package com.github.xml;

import org.apache.commons.lang.StringUtils;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author  Crab2Died
 * <p></p>
 */
public class XMLAnalysis {

    private String filePath;

    @Before
    public void init() {
        this.filePath = "D:\\IdeaSpace\\goblin\\goblin-web\\src\\main\\resources\\config\\msg.xml";
    }

    @Test
    public void testXmlAnalysis() throws DocumentException {
//        SAXReader reader = new SAXReader();
//        File file = new File(filePath);
//        Document doc = reader.read(file);
//        Map map = new HashMap();
//        map.put("ms", "http://www.github.com/schema/goblin");
//        XPath xpath = doc.createXPath("//ms:msg");
//        xpath.setNamespaceURIs(map);
//        List<DefaultElement> list = xpath.selectNodes(doc);
//        for(DefaultElement li : list){
//            li.getData();
//            System.out.println(li.attribute("createDate").getValue());
//        }
    }

    @Test
    public void testAmlAnalysis() {
        DocumentBuilder builder = null;
        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = builder.parse(filePath);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                if (StringUtils.equals(prefix, "ms"))
                    return "http://www.github.com/schema/goblin";
                return null;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                if (StringUtils.equals(namespaceURI, "http://www.github.com/schema/goblin"))
                    return "ms";
                return null;
            }

            @Override
            public Iterator getPrefixes(String namespaceURI) {
                return null;
            }
        });
        try {
            NodeList nodes = (NodeList) xPath.evaluate("//ms:msg", doc, XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element node = (Element) nodes.item(i);
                System.out.println(node.getAttribute("createDate"));
            }

            System.out.println(nodes.getLength());
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }
}
